package com.summer.test.controller;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.context.annotation.Scope;
import org.springframework.dao.DataAccessException;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.summer.core.annotations.ScopeType;
import com.summer.core.controller.EntityHome;
import com.summer.core.controller.LoginUserInfo;
import com.summer.core.util.FacesUtil;
import com.summer.test.model.SystemLoanFee;
import com.summer.test.model.SystemLoanType;
import com.summer.user.model.User;


@Component
@Scope(ScopeType.VIEW)
public class SystemLoanTypeHome extends EntityHome<SystemLoanType>{
	
	@Resource
	HibernateTemplate ht;
	@Resource
	LoginUserInfo loginUserInfo;

	
	/**
	 * 添加修改借款产品
	 */

	@Transactional(readOnly = false, rollbackFor = Exception.class)
	public String save(){
		
		try {
			String loginUserId = loginUserInfo.getLoginUserId();
			User user = getBaseService().get(User.class, loginUserId);
			
			if(getInstance().getId()==null||getInstance().getId().equals(""))
			{
				List<String> list = ht.find(
						"select max(sf.id) from SystemLoanType sf ");
				Integer sltid = list.get(0) == null ?1:Integer.parseInt(list.get(0));
				getInstance().setId(sltid+1+"");
				getInstance().setOperUser(user);
				getInstance().setOperDatetime(new Date());
				getBaseService().save(getInstance());
				FacesUtil.addInfoMessage("保存成功！");
				
			}
			else
			{
				getInstance().setUpdateDatetime(new Date());
				getInstance().setUpdateUser(user);
				getBaseService().merge(getInstance());
				FacesUtil.addInfoMessage("修改成功！");
			}
			
			
			return FacesUtil.redirect("/admin/test/systemLoanType");
		} catch (DataAccessException e) {
			e.printStackTrace();
			FacesUtil.addErrorMessage("保存失败！");
		}
		return null;
	}
	
	
	
	

	
	
}
